#include<bits/stdc++.h>
using namespace std;
int T,n,qz[10000010],now[10];
int sol(int x){
	memset(now,0,sizeof(now));
	while(x){
		now[x%10]++;
		x/=10;
	}
	int res=0;
	for(int i=1;i<=9;i++)
		if(now[res]<=now[i]) res=i;
	return res;
}
int main(){
	freopen("give.in","r",stdin);
	freopen("give.out","w",stdout);
	for(int i=1;i<=10000000;i++)
		qz[i]=qz[i-1]+sol(i);
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		printf("%d\n",qz[n]);
	}
	return 0;
}
